<template>
    <div class="wrap normal">
        <!-- 搜索条件 -->
        <div class="wrap-form" id="wrap-form">
            <el-row :gutter="20">
                <el-col :span="12">
                    <div class="left-panel">
                        <el-form :inline="true" :model="form" label-width="80px" @submit.native.prevent>
                            <el-form-item>
                                <el-input v-model="form.content" placeholder="请输入名称或描述" clearable style="width:220px;"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" icon="el-icon-search" @click="pageOperate('search')">搜索</el-button>
                                <el-button type="primary" icon="el-icon-plus" v-hasPerm="'system:dictionaryManage:add'" @click="pageOperate('add')">添加</el-button>
                                <el-button icon="el-icon-refresh" @click="pageOperate('refresh')">刷新</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                </el-col>
                <el-col :span="12"> 
                    <div class="right-panel">
                        <el-button icon="el-icon-refresh" @click="pageOperate('childRefresh')">刷新</el-button>
                        <el-button type="primary" icon="el-icon-plus" v-hasPerm="'system:dictionaryChild:add'" @click="pageOperate('childAdd')">添加</el-button>
                    </div>
                </el-col>
            </el-row>
        </div>
         <!-- table数据 -->
        <el-row :gutter="20">
            <el-col :span="12">
                <div class="wrap-table">
                    <el-table ref="singleTable" :data="tableData" :max-height="tableMaxHeight" border v-loading="tableLoading" :header-cell-style="{ background: '#F5F6FA' }" highlight-current-row @current-change="changeCurrentRow">
                        <!-- 索引 -->
                        <el-table-column type="index" label="序号" width="60" align="center"> </el-table-column>
                        <el-table-column prop="dictName" label="字典名称" min-width="100"></el-table-column>
                        <el-table-column prop="dictCode" label="字典编码" min-width="100"></el-table-column>
                        <el-table-column prop="status" label="状态" min-width="100">
                            <template slot-scope="scope">
                                <el-tag :type="$options.filters.filterStatusClass(scope.row.status)">{{ scope.row.status | filterStatus }}</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column prop="remark" label="备注" min-width="160"></el-table-column>
                        <el-table-column label="操作" width="100" fixed="right">
                            <template slot-scope="scope">
                                <el-button type="text" v-hasPerm="'system:dictionaryManage:edit'" @click="pageOperate('edit', scope.row)">修改</el-button>
                                <el-button type="text" v-hasPerm="'system:dictionaryManage:delete'" @click="pageOperate('deleteOne', scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :total="page.totalNum" :page.sync="page.pageNum" :limit.sync="page.pageSize" layout="total, sizes, prev, pager, next, jumper" @pagination="getTableData" />
                </div>
            </el-col>
            <el-col :span="12">
                <dictionaryManageData ref="dictionaryManageData" :dictCode="currentRow ? currentRow.dictCode : ''"></dictionaryManageData>
            </el-col>
        </el-row>
        <!-- dialog -->
        <el-dialog v-if="operateDialog" :title="operateTitle" :visible.sync="operateDialog" @close="closeOperateDialog" :width="`${operateWidth}px`" :close-on-press-escape="false" :close-on-click-modal="false">
            <el-form ref="operateForm" :model="operateForm" :rules="operateRules" label-width="100px">
                <el-form-item label="字典名称" prop="dictName">
                    <el-input v-model="operateForm.dictName" placeholder="请输入字典名称" />
                </el-form-item>
                <el-form-item label="字典编码" prop="dictCode">
                    <el-input v-model="operateForm.dictCode" placeholder="请输入字典编码" />
                </el-form-item>
                <el-form-item label="字典状态" prop="status">
                    <el-radio-group v-model="operateForm.status">
                        <el-radio v-for="(item, index) in statusOptions" :label="item.value" :key="index">{{ item.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="operateForm.remark" clearable type="textarea" :rows="3" placeholder="请输入备注"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeOperateDialog">取 消</el-button>
                <el-button type="primary" :loading="operateLoading" @click="operateSubmit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import tableHeight from '@/mixins/tableHeight'
import { statusOptions } from '@/filters/options'
import { getDictionaryList, addOrUpdateDictionary, deleteDictionary } from '@/api/system'
import dictionaryManageData from './components/dictionaryManageData.vue'
export default {
    name: 'DictionaryManage',
    mixins: [tableHeight],
    components: {
        dictionaryManageData
    },
    data() {
        return {
            form: {
                content: ''
            },
            page: {
                pageNum: 1,
                pageSize: 10,
                totalNum: 0
            },
            tableData: [],
            tableLoading: false,
            // 类型
            operateType: '',
            // 标题
            operateTitle: '',
            // 弹框
            operateDialog: false,
            // 宽度
            operateWidth: 550,
            // looading
            operateLoading: false,
            // 操作form
            operateForm: {
                id: '',
                dictName: '',
                dictCode: '',
                status: 0,
                remark: ''
            },
            // 校验规则
            operateRules: {
                dictName: [
                    { required: true, message: '请输入字典名称', trigger: ['blur'] }
                ],
                dictCode: [
                    { required: true, message: '请输入字典编码', trigger: ['blur'] }
                ],
                status: [
                    { required: true, message: '请选择字典状态', trigger: ['change'] }
                ]
            },
            // 状态选项
            statusOptions,
            // 选中行
            currentRow: null
        }
    },
    methods: {
        // 设置选中行
        setCurrentRow(row) {
            this.$refs.singleTable.setCurrentRow(row);
        },
        // 切换选中行
        changeCurrentRow(currentRow) {
            this.currentRow = currentRow
        },
        // 数据
        getTableData() {
            this.tableLoading = true
            const data = {
                pageNum: this.page.pageNum,
                pageSize: this.page.pageSize,
                ...this.form
            }
            getDictionaryList(data).then(res => {
                this.tableLoading = false
                this.tableData = res.data.records
                this.page.totalNum = res.data.total
                this.setCurrentRow(this.tableData[0])
            }).catch(error => {
                this.tableLoading = false
            })
        },
        // 操作
        pageOperate(type, row) {
            this.operateType = type
            switch (type) {
                case 'search':
                    this.page.pageNum = 1
                    this.getTableData()
                    break;
                case 'refresh':
                    Object.assign(this.$data, this.$options.data())
                    this.getTableData()
                    break;
                case 'add':
                    this.operateDialog = true
                    this.operateTitle = '添加字典'
                    break;
                case 'edit':
                    this.operateDialog = true
                    this.operateTitle = '修改字典信息'
                    for (let key in this.operateForm) {
                        this.operateForm[key] = row[key]
                    }
                    break;
                case 'deleteOne':
                    this.$confirm('确认删除该字典项?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        deleteDictionary({ id: row.id }).then(res => {
                            this.getTableData()
                            this.$message_tip(`删除成功`, 'success')
                        }).catch(error => { })
                    }).catch(() => { });
                    break;
                case 'childRefresh':
                    this.$refs.dictionaryManageData.pageOperate('refresh')
                    break;
                case 'childAdd':
                    this.$refs.dictionaryManageData.pageOperate('add')
                    break;
                default:
                    break;
            }
        },
        // 提交
        operateSubmit() {
            this.$refs.operateForm.validate(valid => {
                if (valid) {
                    let formData = JSON.parse(JSON.stringify(this.operateForm))
                    if (this.operateType === 'add' || this.operateType === 'edit') {
                        this.operateLoading = true
                        addOrUpdateDictionary({
                            id: formData.id,
                            dictName: formData.dictName,
                            dictCode: formData.dictCode,
                            status: formData.status,
                            remark: formData.remark,
                        }).then(res => {
                            this.getTableData()
                            this.$message_tip(`${this.operateTitle}成功`, 'success')
                            this.closeOperateDialog()
                        }).catch(error => {
                            this.operateLoading = false
                        })
                    }
                }
            })
        },
        // 关闭操作按钮
        closeOperateDialog() {
            this.operateLoading = false
            this.operateDialog = false
            this.operateTitle = ''
            this.operateType = ''
            this.operateForm = this.$options.data().operateForm
            this.$refs['operateForm'].resetFields()
        }
    },
    mounted() {
        this.getTableData()
    }
}
</script>

<style lang="scss" scoped>
</style>